
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="/js/jquery.min.js"></script>
    <title>购买流量</title>
    <style type="text/css">
    .container {
        width: 100%;
        margin: 20px auto;
    }
    .payTest {
        width: 200px;
        height: 40px;
    }
    </style>
</head>
<body>
    <div class="container">
        <button class="payTest" >test pay</button>
        <div id="code"></div>
    </div>
    <script>
        function onBridgeReady(data){
           WeixinJSBridge.invoke(
               'getBrandWCPayRequest', {
                   "appId":data.appId,     //公众号名称，由商户传入     
                   "timeStamp":data.timeStamp,         //时间戳，自1970年以来的秒数     
                   "nonceStr":data.nonceStr, //随机串     
                   "package":data.package,     
                   "signType":data.signType,         //微信签名方式：     
                   "paySign":data.paySign //微信签名 
               },
               function(res){     
                   if(res.err_msg == "get_brand_wcpay_request：ok" ) {
                        alert("恭喜您，支付成功！");
                        setTimeout(function(){
                            window.location.href = "/user/checkPayment?"+new Date().getTime();
                        },500);
                    }
                }
            ); 
        }

        $(".payTest").click(function(){
            $.ajax({
                url:"/api/device/pay",
                type:"post",
                dataType:"json",
                data:{
                    money:1,
                    flow:2               
                },
                success:function(data){
                    if(data.status.errCode == 0){
                        if (typeof WeixinJSBridge == "undefined"){
                           if( document.addEventListener ){
                               document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                           }else if (document.attachEvent){
                               document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                               document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                           }
                        }else{
                           onBridgeReady(data.data);
                        }
                    }
                    else{
                        alert(data.status.message);
                    }
                }
            });
        });
    </script>
</body>
</html>
